
// pages/reportcard/reportcard.js
import http from '../../utils/requestAnalysis'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectMenu: {},
        switch: true,
        datalist: {},
        classInfo: {},
        singleDepartment: false,
        assignedPoints: true,
        menuList: [

        ],
        subjectAchievement: [],
        height: null,
        type: null
    },
    changeMenu(e) {
        //console.log(e.detail, 'sssssssssssssssss');
        this.setData({
            selectMenu: e.detail
        })
        this.determineSubjectType(e.detail)
        this.getSchoolTranscript()
    },
    changeoverSwitch() {
        this.setData({
            switch: !this.data.switch
        })
    },
    async getSchoolSubject() {
        let that = this
        let datalist = JSON.parse(wx.getStorageSync('alaynisData'))
        //console.log(datalist, 'looooooooooooooooooooooooool')
        if (wx.getStorageSync('newtoken')) {
            let id = JSON.parse(wx.getStorageSync('newtoken')).id
            this.setData({
                datalist: datalist
            })
            let options = {
                url: '/analysis/subject/list',
                method: 'POST',
                data: {
                    navId: datalist.n5,
                    groupId: that.data.type == 1 ? datalist.groupid : that.data.classInfo.classid,
                    groupType: that.data.type == 1 ? 0 : 1
                },
                header: {
                    "content-type": "application/json"
                }
            }
            let res = await http(options)
            //console.info(res)
            if (res.data.code == 0) {
                this.setData({
                    menuList: res.data.data,
                    selectMenu: res.data.data[0]
                })
                this.determineSubjectType(res.data.data[0])
                this.getSchoolTranscript()
                // this.setData({endstartlist:res.data.obj})
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 校级成绩单
    async getSchoolTranscript() {
        let that = this
        if (wx.getStorageSync('newtoken')) {
            if (this.data.singleDepartment) {
                // 单科
                if (this.data.assignedPoints) {
                    // 单科赋分
                    let options = {
                        url: '/analysis/subject/ff/rank',
                        method: 'POST',
                        data: {
                            navId: that.data.selectMenu.navId,
                            groupId: that.data.type == 1 ? that.data.datalist.groupid : that.data.classInfo.classid,
                            stuNameOrExamNo: null,
                            groupType: that.data.type == 1 ? 0 : 1,
                        },
                        header: {
                            "content-type": "application/json"
                        }
                    }
                    let res = await http(options)
                    //console.info(res)
                    if (res.data.code == 0) {
                        this.setData({
                            subjectAchievement: res.data.data
                        })
                        // this.setData({endstartlist:res.data.obj})
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'success',
                            duration: 2000 //持续的时间
                        })
                    }
                } else {
                    // 单科不赋分
                    let options = {
                        url: '/analysis/class/small/question/score',
                        method: 'POST',
                        data: {
                            navId: that.data.selectMenu.navId,
                            groupId: that.data.type == 1 ? that.data.datalist.groupid : that.data.classInfo.classid,
                            stuNameOrExamNo: null,
                            groupType: that.data.type == 1 ? 1 : 0
                        },
                        header: {
                            "content-type": "application/json"
                        }
                    }
                    let res = await http(options)
                    //console.info(res)
                    if (res.data.code == 0) {
                        this.setData({
                            subjectAchievement: res.data.data
                        })
                        // this.setData({endstartlist:res.data.obj})
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'success',
                            duration: 2000 //持续的时间
                        })
                    }
                }
            } else {
                // 总分
                let options = {
                    url: '/analysis/allScore/direction/rank',
                    method: 'POST',
                    data: {
                        navId: that.data.datalist.n5,
                        groupId: that.data.type == 1 ? that.data.datalist.groupid : that.data.classInfo.classid,
                        stuNameOrExamNo: null,
                        groupType: that.data.type == 1 ? 0 : 1,
                    },
                    header: {
                        "content-type": "application/json"
                    }
                }
                if (that.data.selectMenu.type4 != null) {
                    options.data.type4 = [that.data.selectMenu.type4]
                }
                if (!this.data.assignedPoints && !this.data.singleDepartment) {
                    options.data.navType = 0
                }
                let res = await http(options)
                //console.info(res)
                if (res.data.code == 0) {
                    this.setData({
                        subjectAchievement: res.data.data
                    })
                    // this.setData({endstartlist:res.data.obj})
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'success',
                        duration: 2000 //持续的时间
                    })
                }
            }
        } else {
            wx.redirectTo({
                url: '/pages/newteacherlogin/newteacherlogin',
            })
        }
    },
    // 判断科目类型
    determineSubjectType(e) {
        if (e.subjectName.indexOf("总分") > -1) {
            this.setData({
                singleDepartment: false
            })
        } else {
            this.setData({
                singleDepartment: true
            })
        }
        if (e.navType == 1) {
            this.setData({
                assignedPoints: true
            })
        } else {
            this.setData({
                assignedPoints: false
            })
        }
    },
    lookAnswerSheet(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/answerSheet/answerSheet?id=${id}&navId=${this.data.selectMenu.navId}&role=1`,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            type: options.type
        })
        if (options.type == 2) {
            this.setData({
                classInfo: JSON.parse(wx.getStorageSync('classInfo'))
            })
        }
        this.getSchoolSubject()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let query = wx.createSelectorQuery()
        query.select('#myElement').boundingClientRect((rect) => {
            //console.log(rect.top, 'ssaaaaaaaaaaaaaaaa') //距离顶部距离
            this.setData({
                height: rect.top + 10 + 'px'
            })

        }).exec()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})